package com.xydtech.ecard.merchant.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.merchant.domain.EcardMerchantPartInfo;
import com.xydtech.ecard.merchant.service.IEcardMerchantPartInfoService;
import com.xydtech.ecard.merchant.utils.MyExcelUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子名片商户成员Controller
 *
 * @author xydtech
 * @date 2023-08-01
 */
@Controller
@RequestMapping("/merchant/info/part")
public class EcardMerchantPartInfoController extends BaseController
{
    private String prefix = "merchant";

    @Autowired
    private IEcardMerchantPartInfoService ecardMerchantPartInfoService;

    //@RequiresPermissions("merchant:part:view")
    @GetMapping("/{merchantId}/{merchantType}")
    public String part(@PathVariable("merchantId") String merchantId, @PathVariable("merchantType") String merchantType, ModelMap mmap)
    {
        mmap.put("merchantId", merchantId);
        mmap.put("merchantType", merchantType);
        return prefix + "/part";
    }

    /**
     * 查询电子名片商户成员列表
     */
    //@RequiresPermissions("merchant:part:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardMerchantPartInfo ecardMerchantPartInfo)
    {
        String salt = getSalt();//盐值
        startPage();
        List<EcardMerchantPartInfo> list = ecardMerchantPartInfoService.selectEcardMerchantPartInfoList(ecardMerchantPartInfo, salt);
        return getDataTable(list);
    }

    /**
     * 导出电子名片商户成员列表
     */
    //@RequiresPermissions("merchant:part:export")
    @GetMapping("/export/{dataParam}")
    public void export(@PathVariable("dataParam") String dataParam, HttpServletResponse response)
    {
        String salt = getSalt();//盐值
        EcardMerchantPartInfo ecardMerchantPartInfo = new EcardMerchantPartInfo();
        JSONArray parsed = (JSONArray) JSONObject.parse(dataParam);
        String ids = "";
        String merchantType = "";////商户类型：1-个人，2-商户，3-企业，4-新型农业主体
        Map<String, Object> paramMap = new HashMap<>();
        for (Object o : parsed) {
            JSONObject jsonObject = JSONObject.parseObject(String.valueOf(o));
            if ("merchantType".equals(jsonObject.getString("name"))){
                merchantType = jsonObject.getString("value");
                ecardMerchantPartInfo.setMerchantType(merchantType);
            } else if ("merchantId".equals(jsonObject.getString("name"))){
                ecardMerchantPartInfo.setMerchantId(jsonObject.getLongValue("value"));
            }else if ("ids".equals(jsonObject.getString("name"))){
                ids = jsonObject.getString("value");
            }
        }
        List<EcardMerchantPartInfo> list = new ArrayList<>();
        //判断ids是否为空，不为空则导出选择的数据
        if (StringUtils.objIsNullOrEmpty(ids)){
            list = ecardMerchantPartInfoService.selectEcardMerchantPartInfoListByExport(ecardMerchantPartInfo, salt);
        } else {
            ids = ids.replaceAll("\\[","").replaceAll("]","");
            list = ecardMerchantPartInfoService.selectEcardMerchantPartInfoListByIds(ids, merchantType, salt);
        }
        MyExcelUtils.exportExcel(list,
                "电子名片商户成员信息",
                "电子名片商户成员",
                EcardMerchantPartInfo.class,
                "电子名片商户成员信息文件" + System.currentTimeMillis() + ".xlsx", response);
    }

    /**
     * 新增电子名片商户成员
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片商户成员
     */
    @RequiresPermissions("merchant:part:add")
    @Log(title = "电子名片商户成员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardMerchantPartInfo ecardMerchantPartInfo)
    {
        String salt = getSalt();//盐值
        return toAjax(ecardMerchantPartInfoService.insertEcardMerchantPartInfo(ecardMerchantPartInfo, salt));
    }

    /**
     * 修改电子名片商户成员
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        String salt = getSalt();//盐值
        EcardMerchantPartInfo ecardMerchantPartInfo = ecardMerchantPartInfoService.selectEcardMerchantPartInfoById(id, salt);
        mmap.put("ecardMerchantPartInfo", ecardMerchantPartInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片商户成员
     */
    @RequiresPermissions("merchant:part:edit")
    @Log(title = "电子名片商户成员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardMerchantPartInfo ecardMerchantPartInfo)
    {
        String salt = getSalt();//盐值
        return toAjax(ecardMerchantPartInfoService.updateEcardMerchantPartInfo(ecardMerchantPartInfo, salt));
    }

    /**
     * 删除电子名片商户成员
     */
    @RequiresPermissions("merchant:part:remove")
    @Log(title = "电子名片商户成员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardMerchantPartInfoService.deleteEcardMerchantPartInfoByIds(ids));
    }
}
